﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UdrinkWater_Project.Officer
{
    public partial class AddList_Unpaid : System.Web.UI.Page
    {
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
           // txt_Name.Focus();
        }

        protected void ASPxGridView1_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
        {
            if (e.CommandArgs.CommandName == "Cmd_Add")
            {
                //object Order_Date = GV_ShowOrder.GetRowValues(e.CommandArgs.CommandArgument, "Order_Date");
                int Index = GV_ShowOrder.FindVisibleIndexByKeyValue(e.KeyValue);
                object Order_ID = GV_ShowOrder.GetRowValues(Index, "Order_Date");
                string Order_Date = Convert.ToDateTime(Order_ID).ToString("yyyy-MM-dd HH:mm:ss.fff", new System.Globalization.CultureInfo("en-US"));
                DataTable dt = GetDataOrder(Order_Date);
                txt_Referent.Text = Order_Date;
                txt_Name.Text = dt.Rows[0]["Customer_ID"].ToString();
                txt_Address.Text = dt.Rows[0]["Delivery_Address"].ToString();
                Com_Province.Value = Convert.ToInt32(dt.Rows[0]["Province_ID"]);
                Com_Aumphur.Value = Convert.ToInt32(dt.Rows[0]["Aumphur_ID"]);
                Com_District.Value = Convert.ToInt32(dt.Rows[0]["District_ID"]);

                pcLogin.ShowOnPageLoad = false;
            }
        }
        private DataTable GetDataOrder(string Order_Date)
        {
            string sqltext = @"SELECT * FROM TB_Order WHERE Order_Date ='" + Order_Date + "' ";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];

        }
        protected void bt_Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("UnpaidList.aspx");
        }

        protected void Com_Province_SelectedIndexChanged(object sender, EventArgs e)
        {
            Com_Aumphur.Text = "";
            Com_District.Text = "";
        }

        protected void Com_Aumphur_SelectedIndexChanged(object sender, EventArgs e)
        {
            Com_District.Text = "";
        }

        protected void bt_SaveUnpaid_Click(object sender, EventArgs e)
        {
            if (txt_Referent.Text !="")
            {
                InsertsUnpaid(txt_Referent.Text);
                Update_Status(txt_Referent.Text);
            }
            txt_Referent.Text = "";
            txt_Name.Text = "";
            txt_Count.Text = "";
            txt_Address.Text = "";
            Com_District.Text = "";
            Com_Aumphur.Text = "";
            Com_Province.Text = "";
            Response.Redirect("UnpaidList.aspx");
        }
        public static object GetDataValue(object value)
        {
            if (value == null)
            {
                return DBNull.Value;
            }
            return value;
        }
        public void Update_Status(string Date_ID)//ส่ง session พนักงาน
        {
            string sqltext = @"UPDATE [TB_Order] SET                      
                                     [Status_Unpaid]=2
                                     WHERE [Order_Date]=@Order_Date";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@Order_Date", GetDataValue(Date_ID));

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        public void InsertsUnpaid(string Order_ID)
        {
            string sqltext = @"insert into [TB_unpaid] 
                                   ( [Unpaid_Date]
                                    ,[Name]
                                    ,[Address]
                                    ,[District_ID]
                                    ,[Aumphur_ID]
                                    ,[Province_ID]
                                    ,[Count]
                                    ,[UnpaidDeadline_Date]
                                    ,[Act_Officer]
                                    ,[Status_Unpaid]
                                    ,[Referent_Unpaid])
                                   values (@Date,@Name,@Address,@District,@Aumphur,@Province,@Count,@Deadline,@Act_Off,2,@Ref)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@Date", GetDataValue(DateTime.Now));
            sqlcomm.Parameters.AddWithValue("@Name", GetDataValue(txt_Name.Text));
            sqlcomm.Parameters.AddWithValue("@Address", GetDataValue(txt_Address.Text));
            sqlcomm.Parameters.AddWithValue("@District", GetDataValue(Com_District.Value));
            sqlcomm.Parameters.AddWithValue("@Aumphur", GetDataValue(Com_Aumphur.Value));
            sqlcomm.Parameters.AddWithValue("@Province", GetDataValue(Com_Province.Value));
            sqlcomm.Parameters.AddWithValue("@Count", GetDataValue(txt_Count.Text));
            sqlcomm.Parameters.AddWithValue("@Deadline", GetDataValue(DateEdit_Deadline.Value));
            sqlcomm.Parameters.AddWithValue("@Act_Off", GetDataValue(Session["User"].ToString()));
            sqlcomm.Parameters.AddWithValue("@Ref", GetDataValue(Order_ID));

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {
                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
    }
}